public class MyLinkList {
    static class ListNode{
        public int val;//节点的值域
        public ListNode next;//下一个节点的地址

        public ListNode(){

        }
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;//表示当前链表的头结点

    //初始化链表
    public void createNode(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }

    //头插法
    public void addFirst(int val){
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int val){
        ListNode cur = head;
        ListNode node = new ListNode(val);
        if(cur == null){
            //如果链表中没有元素，那么直接让头结点指向node
            head = node;
            return;
        }
        //当cur.next == null时，该结点就是尾巴节点
        //cur == null证明该链表已经被遍历完了
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //获取单链表的长度
    public int size(){
        ListNode cur = head;
        int lenth = 0;
        while(cur!=null){
            cur = cur.next;
            lenth++;
        }
        return lenth;
    }
    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            throw new RuntimeException("输入下标有误");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        //1、定义cur走index-1步
        //2、进行插入
        ListNode node = new ListNode(data);
        ListNode cur = head;
        int count = index-1;
        while(count != 0){
            cur = cur.next;
            count--;
        }
        node.next = cur.next;
        cur.next = node;
    }

    //查找关键字key是否在单链表中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现的关键字为key的节点
    public  void remove(int key){
        if(head == null){
            return;
        }
        //单独删除头节点
        if(head.val == key){
            head = head.next;
            return;
        }
        //找到删除节点的前一个节点
        ListNode cur = head;
        while(cur.next != null){
            ListNode del = cur.next;
            if(del.val == key){//cur指向的是被删除节点的前一个节点
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
        if(cur == null){
            System.out.print("链表里没有你要删除的数字");
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        //prev用来定位被删除节点的前一个元素
        //cur用来遍历链表和找到被删除元素
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
            //单独删除头节点
            if(head.val == key){
                head = head.next;
            }
        }
    }
    //清空链表
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            cur = null;
            cur = cur.next;
        }
    }
    //打印链表
    public void  display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
}
